<?php

//周年庆盲盒获得配置
namespace App\Http\Controllers\Admin;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class AnniversaryBlindBoxController extends BaseController
{
    /**
     * @api {Get} /anniversaryblindbox/index 周年庆活动
     * @apiGroup 补充接口-盲盒管理
     *
     * @apiParam {Int} type 1：普通用户(默认)；2：标记用户
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "max_blind_box_number": "89",
    "list": [
    {
    "id": 1, 序号
    "user_type": 1,
    "recharge_money": 0, 充值金额
    "max_number": 5, 最大盲盒数量
    "ratio": 100, 中奖概率，单位%
    "current_number": 177, 当前抽取数量
    "add_time": "2021-04-06 15:16:34" 创建时间
    },
    {
    "id": 8,
    "user_type": 1,
    "recharge_money": 0,
    "max_number": 1,
    "ratio": 0,
    "current_number": 0,
    "add_time": "2021-04-08 10:17:25"
    }
    ]
    }
    }
     */
    public function index(Request $request)
    {
        $type = $request->type ?: 1; //1：普通用户；2：标记用户

        $list = app('model_anniversary_blind_box')->where(['user_type' => $type])->get();
        foreach ($list as &$v) {
            $v->add_time = date('Y-m-d H:i:s', $v->add_time);
        }

        $max_blind_box_number = $type == 1 ? app('model_config')->where('key', 'ACT_ANNIVERSARY_BLIND_BOX_TOTAL')->value('value') : app('model_config')->where('key', 'ACT_ANNIVERSARY_BLIND_BOX_TOTAL_SPECIAL')->value('value');

        return Response::sendData(['max_blind_box_number' => (int)$max_blind_box_number, 'list' => $list]);
    }

    /**
     * @api {Put} /anniversaryblindbox/updatemaxnum 更新可抽盲盒数量
     * @apiGroup 补充接口-盲盒管理
     *
     * @apiParam {Int} type 1：普通用户；2：标记用户
     * @apiParam {Int} number 可抽取数量
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function updateMaxNum(Request $request)
    {
        $type = $request->type ?: 1; //1：普通用户；2：标记用户
        $num = $request->num; //可抽取数量
        if (empty($num) || !is_numeric($num) || $num < 0) {
            throw new BasicException(10001);
        }
        $key = $type == 1 ? 'ACT_ANNIVERSARY_BLIND_BOX_TOTAL' : 'ACT_ANNIVERSARY_BLIND_BOX_TOTAL_SPECIAL';
        $origin_number = app('model_config')->where('key', $key)->value('value');
        $origin_number = $origin_number ?: 0;
        $c = app('model_config')->where(['key' => $key])->first();
        $desc = '周年庆'. ($type == 2 ? '标记用户' : '普通用户') . '可抽盲盒数量';
        if (!$c) {
            app('model_config')->add([
                'key'    => $key,
                'value'  => $num,
                'type'   => 0,
                'group'  => 100,
                'name'   => $desc,
                'desc'   => $desc,
                'status' => 0,
                'sort'   => 0,
            ]);
        } else {
            app('model_config')->where(['key' => $key])->update(['value' => $num]);
        }

        //删除redis缓存
        app('redis')->del('DB_CONFIG_DATA');
        return Response::sendData([]);
    }

    /**
     * @api {Post} /anniversaryblindbox/add 添加可抽取盲盒
     * @apiGroup 补充接口-盲盒管理
     *
     * @apiParam {Int} type 1：普通用户；2：标记用户
     * @apiParam {Int} recharge_money 充值金额
     * @apiParam {Int} max_num 最大数量
     * @apiParam {Int} probability 概率
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function add(Request $request)
    {
        $type = $request->type;
        $rechargeMoney = $request->recharge_money;
        $maxNum = $request->max_num;
        $probability = $request->probability;

        if ($type != 1 && $type != 2) {
            throw new BasicException(10001);
        }

        $this->paramVerify($rechargeMoney, $maxNum, $probability);

        $res = app('model_anniversary_blind_box')
            ->insert([
                'user_type' => $type,
                'recharge_money' => $rechargeMoney,
                'max_number' => $maxNum,
                'ratio' => $probability,
                'add_time' => time(),
            ]);
        if ($res) {
            return Response::sendData([]);
        } else {
            throw new BasicException(0, '添加失败');
        }
    }

    /**
     * @api {Put} /anniversaryblindbox/edit 编辑可抽取盲盒
     * @apiGroup 补充接口-盲盒管理
     *
     * @apiParam {Int} id 序号
     * @apiParam {Int} recharge_money 充值金额
     * @apiParam {Int} max_num 最大数量
     * @apiParam {Int} probability 概率
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function edit(Request $request)
    {
        $id = $request->id;
        $rechargeMoney = $request->recharge_money;
        $maxNum = $request->max_num;
        $probability = $request->probability;

        $record = app('model_anniversary_blind_box')->select('id')->where('id', $id)->first();
        if (empty($record)) {
            throw new BasicException(10001);
        }

        $this->paramVerify($rechargeMoney, $maxNum, $probability);
        $res = app('model_anniversary_blind_box')->where('id', $id)
            ->update([
                'recharge_money' => $rechargeMoney,
                'max_number' => $maxNum,
                'ratio' => $probability,
            ]);
        if ($res) {
            return Response::sendData([]);
        } else {
            throw new BasicException(0, '编辑失败');
        }
    }

    private function paramVerify($rechargeMoney, $maxNum, $probability)
    {
        if (empty($rechargeMoney) || !is_numeric($rechargeMoney) || $rechargeMoney < 0) {
            throw new BasicException(10001);
        }
        if (empty($maxNum) || !is_numeric($maxNum) || $maxNum < 0) {
            throw new BasicException(10001);
        }
        if (empty($probability) || !is_numeric($probability) || $probability < 0 || $probability > 100) {
            throw new BasicException(0, '概率必须是0-100的数字');
        }
    }
}